package com.qububu.db.dao.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.sql.DataSource;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.qububu.db.dao.DinePeopleDao;
import com.qububu.play.dineTogether.dto.ActionPeopleDto;
import com.qububu.utils.ParamConverter;
import com.qububu.utils.SystemUtil;

@Repository
public class DinePeopleDaoImpl implements DinePeopleDao{
	
	private JdbcTemplate jdbcTemplate;
	
	public void setDataSource(DataSource dataSource) {
		this.jdbcTemplate = new JdbcTemplate(dataSource);
	}
	
	public List<ActionPeopleDto> getActionPeopleListByActionId(int actionIdInt) {
		List<ActionPeopleDto> peopleList = null;
		peopleList = new ArrayList<ActionPeopleDto>();
		
		String sql = "select * from action_people where action_id = ?";
		List<Map<String, Object>> peopleMap = jdbcTemplate.queryForList(sql, actionIdInt);
		ActionPeopleDto actionPeopleDto = null;
		for (Map<String, Object> elem : peopleMap) {
			actionPeopleDto = new ActionPeopleDto((Long)elem.get("people_id"), (Long)elem.get("staff_id"), (Long)elem.get("action_id"));
			actionPeopleDto.setPeopleName(String.valueOf(elem.get("people_name")));
			peopleList.add(actionPeopleDto);
		}
		
		return peopleList;
	}

	public void doAddActionPeople(ActionPeopleDto actionPeopleDto) {
		String addPeopleSql = "insert into action_people(people_id, people_name, staff_id, action_id) values(?, ?, ?, ?)";
		
		long peopleId = ParamConverter.convertToLongObj(SystemUtil.decrypt(actionPeopleDto.getPeopleId()));
//		peopleId = SystemUtil.decrypt(peopleId);
		long staffId = ParamConverter.convertToLongObj(SystemUtil.decrypt(actionPeopleDto.getStaffId()));
//		staffId = SystemUtil.decrypt(staffId);
		long actionId = ParamConverter.convertToLongObj(SystemUtil.decrypt(actionPeopleDto.getActionId()));
//		actionId = SystemUtil.decrypt(actionId);
		
		Object[] addPeopleVal = {
				peopleId, actionPeopleDto.getPeopleName(), staffId, actionId
		};
		jdbcTemplate.update(addPeopleSql, addPeopleVal);
	}
	
	

	public void doModifyActionPeople(ActionPeopleDto actionPeopleDto) {
		// TODO Auto-generated method stub
		String sql = "update action_people set people_name = ? where food_id = ?";

		jdbcTemplate.update(sql, new Object[]{actionPeopleDto.getPeopleName(), actionPeopleDto.getPeopleId()});
	}

	public boolean doDeleteActionPeopleByPeopleId(int peopleIdInt) {
		String sql = "delete from action_people where people_id = ?";
		jdbcTemplate.update(sql, peopleIdInt);
		
		return true;
	}

	public ActionPeopleDto getActionPeopleInfoByPeopleId(int peopleIdInt) {
		String sql = "select * from action_people where people_id = ?";
		
		List<Map<String, Object>> peopleInfoList = jdbcTemplate.queryForList(sql, peopleIdInt);
		
		ActionPeopleDto actionPeopleDto = null;
		if (!peopleInfoList.isEmpty() && peopleInfoList.size() > 0) {
			Map<String, Object> peopleInfoMap = peopleInfoList.get(0);
			actionPeopleDto = new ActionPeopleDto((Long)peopleInfoMap.get("people_id"), (Long)peopleInfoMap.get("staff_id"), (Long)peopleInfoMap.get("action_id"));
			actionPeopleDto.setPeopleName(String.valueOf(peopleInfoMap.get("people_name")));	
		}
		
		return actionPeopleDto;
	}

	public void changePeopleStaff(int peopleId, int staffId) {
		String sql = "update action_people set staff_id=? where people_id=?";
		jdbcTemplate.update(sql, new Object[]{staffId, peopleId});
	}

}
